package com.learn.concurrent.blockingqueue;

import ch.qos.logback.core.util.TimeUtil;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author wangxing
 * @version 2020/8/17 6:20 Administrator
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        int BOUND = 10;
        int N_PRODUCERS = 8;
//        线程处理数量
        int N_CONSUMERS = Runtime.getRuntime().availableProcessors();
        System.out.println(N_CONSUMERS);
        TimeUnit.SECONDS.sleep(2);
        int poisonPill = Integer.MAX_VALUE;
        int poisonPillPerProducer = N_CONSUMERS / N_PRODUCERS;
        int mod = N_CONSUMERS / N_PRODUCERS;
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(BOUND);
        for (int i = 0; i < N_PRODUCERS; i++) {
            new Thread(new NumberProducer(queue, poisonPill, poisonPillPerProducer+mod)).start();
//            new Thread(new NumberProducer(queue, poisonPill, poisonPillPerProducer)).start();
        }
        for (int i = 0; i < N_CONSUMERS; i++) {
            new Thread(new NumberConsumer(queue, poisonPill)).start();
        }
    }
}
